package com.studentmanage.manage_score.service;

import com.studentmanage.framework.domain.base.Score;
import com.studentmanage.framework.domain.base.ScoreExt;
import com.studentmanage.framework.domain.base.ScoreExt2;
import com.studentmanage.framework.domain.base.Student;
import com.studentmanage.framework.domain.base.request.ScoreQueryRequest;
import com.studentmanage.framework.domain.common.response.CommonCode;
import com.studentmanage.framework.domain.common.response.QueryResult;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.framework.utils.PageSizeUtil;
import com.studentmanage.manage_score.client.StudentClient;
import com.studentmanage.manage_score.dao.ScoreExtMapper;
import com.studentmanage.manage_score.dao.ScoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScoreService {
    @Autowired
    private ScoreRepository scoreRepository;
    @Autowired
    private StudentClient studentClient;
    @Autowired(required = false)
    private ScoreExtMapper scoreMapper;

    //根据学生查询成绩列表
    public QueryResult<ScoreExt> findScoreStudentList(String studentId, int page, int size, ScoreQueryRequest scoreQueryRequest) {
        scoreQueryRequest.setPage(PageSizeUtil.formatPage(page,size));
        scoreQueryRequest.setSize(PageSizeUtil.formatSize(size));
        List<ScoreExt> list = scoreMapper.findListByStudent(studentId, scoreQueryRequest);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        Integer total = scoreMapper.getTotalByStudent(studentId, scoreQueryRequest);
        queryResult.setTotal(total);
        return queryResult;
    }

    //增加分数记录
    public ResponseResult addCourse(Score score) {
        if(score == null || score.getCourseId() == null || score.getStudentId() == null){
            return new ResponseResult(CommonCode.FAIL);
        }
        ResponseValue<Student> responseValue = studentClient.findStudentById(score.getStudentId());
        if(!responseValue.isSuccess()){
            return new ResponseResult(CommonCode.STUDENT_ID_NOT_EXIST);
        }
        int point = Integer.parseInt(score.getFinalScore());
        if(point >= 60){
            score.setPassTag("是");
        }else{
            score.setPassTag("否");
        }
        scoreRepository.save(score);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //根据课程查询成绩列表
    public QueryResult<ScoreExt2> findScoreCourseList(String courseId, int page, int size, ScoreQueryRequest scoreQueryRequest) {
        scoreQueryRequest.setPage(PageSizeUtil.formatPage(page,size));
        scoreQueryRequest.setSize(PageSizeUtil.formatSize(size));
        List<ScoreExt2> list = scoreMapper.findListByCourse(courseId, scoreQueryRequest);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        Integer total = scoreMapper.getTotalByCourse(courseId, scoreQueryRequest);
        queryResult.setTotal(total);
        return queryResult;
    }

    //查询导出的excl列表
    public QueryResult<ScoreExt> findScoreExclList(String studentId) {
        List<ScoreExt> exclList = scoreMapper.findExclList(studentId);
        QueryResult<ScoreExt> queryResult = new QueryResult<>();
        queryResult.setList(exclList);
        queryResult.setTotal(exclList.size());
        return queryResult;
    }

    public ResponseResult editScore(Score score) {
        if (score == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        scoreRepository.save(score);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult deleteScore(Score score) {
        if(score == null){
            return new ResponseResult(CommonCode.FAIL);
        }
        scoreRepository.deleteById(score.getId());
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
